package com.zrspring.libv2.base;

/*   
 * Copyright (c) 2014-2014 China.ChongQing.MaiQuan Ltd All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.zrspring.libv2.R;
import butterknife.ButterKnife;

/**
 * @author zrspring
 * @ClassName: BaseFragment
 * @Description: 基础的Fragment
 * @date 2015-11-24 下午3:52:30
 * @ConputerUserName zrspring
 */

public abstract class BaseFragment extends Fragment {

    private ProgressDialog progressDialog;
    private Dialog networkConnectErrorDialog;

    /**
     * @Variables savedBudleData : 用于保存数据的bundle
     */
    public Bundle savedBudleData = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        bindingListener(activity);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        findBundleData(getArguments());
        LoadData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(setFragmentViewLayout(), container, false);
        ButterKnife.bind(this, fragmentView);
        if (savedInstanceState != null && savedBudleData == null) {
            savedBudleData = savedInstanceState.getBundle(getSaveDataBundleKeyName());
        }
        if (savedBudleData != null) {
            readBundleFromSaveInstance(savedBudleData);
        }
        iniView(fragmentView);
        savedBudleData = null;
        return fragmentView;
    }

    /**
     * @return
     * @throws
     * @Title: getSaveDataBundleKeyName
     * @Description: 给储存数据的Bundle的Key名
     */
    public abstract String getSaveDataBundleKeyName();

    /**
     * @param
     * @throws
     * @Title: resetDataToView
     * @Description: 如果View被从新加载，在那么就从savedInstanceState中取出指定的 bundle对象所保数据的，交给具体的实现类处理,需要检查是否为空
     */
    public abstract void readBundleFromSaveInstance(Bundle dataBundle);

    /**
     * @return 返回一个已经储存数据的Bundle
     * @throws
     * @Title: needSaveData
     * @Description: 将需要保存的数据，在此方法中去实现
     */
    public abstract Bundle needSaveData();


    @Override
    public void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
        // outState.putBundle(getSaveDataBundleKeyName(), (savedBudleData != null) ? savedBudleData : needSaveData());
        //activity 销毁的时候不保存 Fragment状态
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        // savedBudleData = needSaveData();
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
        if (networkConnectErrorDialog != null) {
            networkConnectErrorDialog.dismiss();
            networkConnectErrorDialog = null;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        cleanMemory();
        closeRetryDialog();
    }


    //public abstract int chinlFragmentClean();

    /**
     * @param activity
     * @return
     * @throws
     * @Title: bindingListener
     * @Description: 绑定和Activity的接口
     */
    public abstract int bindingListener(Activity activity);

    /**
     * @return
     * @throws
     * @Title: setFragmentView
     * @Description: 设置布局
     */
    public abstract int setFragmentViewLayout();

    /**
     * @param fragmentView
     * @throws
     * @Title: iniView
     * @Description: 初始化布局
     */
    public abstract void iniView(View fragmentView);

    /**
     * @param data
     * @throws
     * @Title: findBundleData
     * @Description: 获取activity传过来的数据
     */
    public abstract void findBundleData(Bundle data);

    /**
     * @throws
     * @Title: LoadData
     * @Description: 加载数据，本地，或者服务器
     */
    public abstract void LoadData();

    /**
     * @throws
     * @Title: cleanMemory
     * @Description: 清空内存
     */
    public abstract void cleanMemory();

    /**
     * @param msg
     * @throws
     * @Title: showToast
     * @Description: 弹出消息
     */
    public void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int strResId) {
        Toast.makeText(getActivity(), getResources().getString(strResId), Toast.LENGTH_SHORT).show();
    }
    /**
     * @throws
     * @Title: showToastInDevelopment
     * @Description: 显示在开发中
     */
    public void showToastIsDevelopmenting() {
        Toast.makeText(getActivity(), "功能正在开发中", Toast.LENGTH_SHORT).show();
    }

    /**
     * @Title: openProgressDialog @Description: 打开进度条 @param title @param
     * desc @throws
     */
    protected void openProgressDialog(String desc) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setMessage(desc);
            progressDialog.setCancelable(true);
            progressDialog.show();
        } else {
            progressDialog.show();
        }
    }

    protected void openProgressDialog(int strResId) {
        String message = getResources().getString(strResId);
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setMessage(message);
            progressDialog.setCancelable(true);
            progressDialog.show();
        } else {
            progressDialog.show();
        }
    }

    /**
     * @Title: closeProgressDialog @Description: 关闭进度条 @throws
     */
    protected void closeProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    protected void setProgressMessage(String message) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.setMessage(message);
        }
    }

    public void openRetryDialog(
            String msg,
            boolean canCelable,
            DialogInterface.OnClickListener retryListener,
            DialogInterface.OnClickListener exitListener) {
        if (networkConnectErrorDialog == null) {
            networkConnectErrorDialog = new AlertDialog.Builder(getActivity()).
                    setMessage(msg).
                    setPositiveButton(getResources().getString(R.string.lib_no_net_retry), retryListener).
                    setNegativeButton(getResources().getString(R.string.lib_no_net_exit), exitListener).create();
        }
        networkConnectErrorDialog.setCancelable(canCelable);
        networkConnectErrorDialog.show();
    }

    public void closeRetryDialog() {
        if (networkConnectErrorDialog != null) {
            networkConnectErrorDialog.dismiss();
            networkConnectErrorDialog = null;
        }
    }
}
